#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Use sreport to get cluster usage for the first 3 days from
#          configuration set up in test22.1.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2008 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# Portions Copyright (C) 2012-2017 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################

proc inc22_1_3 { } {

	global sreport exit_code node0_down_start node0_down_end node0_cpus
	global node1_cpus cluster1 cluster2 my_pid accounts users cluster_cpus
	global job0_start job1_end job1_cpus job1_alloc job2_start
	global job2_end job2_cpus job2_alloc job2_elig job3_start
	global job3_end job3_cpus job3_alloc job3_elig acct1_alloc acct2_alloc
	global acct3_alloc total_alloc wckey1_alloc user1_wckey1_alloc
	global user2_wckey1_alloc user1 user2 account1 account2 account3
	global sql_rem_clus1 wckey1 wc_key_track

	#since there are 2 test clusters we will just use one
	set cluster $cluster1
	set sql_rm $sql_rem_clus1

	# Mon Dec 31 23:00:00 2007
	set start_time "Mon Dec 31 23:00:00 2007"
	set period_start [exec date +%s --date=$start_time]

	# Tue Jan 3 00:00:00 2008
	set end_time "Tue Jan 3 00:00:00 2008"
	set period_end [exec date +%s --date=$end_time]

	set start_str [timestamp -format %Y-%m-%dT%X -seconds $period_start]
	set end_str [timestamp -format %Y-%m-%dT%X -seconds $period_end]

	set reported [expr ($period_end - $period_start) * $cluster_cpus]
	set down [expr ($node0_down_end - $node0_down_start) * $node0_cpus]
	set alloc_sec [expr ($job1_end-$job0_start) * $job1_cpus]
	set alloc_sec [expr $alloc_sec + (($job2_end-$job2_start) * $job2_cpus)]
	set alloc_sec [expr $alloc_sec + (($job3_end-$job3_start) * $job3_cpus)]
	set wckey_alloc_sec1 [expr $job1_alloc + $job3_alloc]
	set wckey_alloc_sec2 $job2_alloc
	set resv [expr ($job2_start - $job2_elig) * $job2_cpus]
	set resv [expr $resv + (($job3_start - $job3_elig) * $job3_cpus)]
	# I didn't have time to do the correct math here so I am just putting in 9000 which should be the correct value of over commit
	set over 9000
	set resv [expr $resv - $over]
	set idle [expr $reported - ($down + $alloc_sec + $resv)]
	# do the same logic inside the plugin to figure out the correct
	# idle time and resv time
	if {$idle < 0 } {
		set resv [expr $resv + $idle]
		set idle 0
		if {$resv < 0} {
			set resv 0
		}
	}
	set down [format "%d\\\(%.2f%%\\\)" $down [expr double($down * 100)/$reported]]
	set alloc [format "%d\\\(%.2f%%\\\)" $alloc_sec [expr double($alloc_sec * 100)/$reported]]
	set resv [format "%d\\\(%.2f%%\\\)" $resv  [expr double($resv * 100)/$reported]]
	set idle [format "%d\\\(%.2f%%\\\)" $idle [expr double($idle * 100)/$reported]]
	set over [format "%d\\\(%.2f%%\\\)" $over [expr double($over * 100)/$reported]]

	set job1_alloc_str [format "%d\\\(%.2f%%\\\)" $job1_alloc [expr double($job1_alloc * 100)/$reported]]
	set job2_alloc_str [format "%d\\\(%.2f%%\\\)" $job2_alloc [expr double($job2_alloc * 100)/$reported]]
	set job3_alloc_str [format "%d\\\(%.2f%%\\\)" $job3_alloc [expr double($job3_alloc * 100)/$reported]]
	set total_alloc_str [format "%d\\\(%.2f%%\\\)" $total_alloc [expr double($total_alloc * 100)/$reported]]
	set acct1_alloc_str [format "%d\\\(%.2f%%\\\)" $acct1_alloc [expr double($acct1_alloc * 100)/$reported]]
	set acct2_alloc_str [format "%d\\\(%.2f%%\\\)" $acct2_alloc [expr double($acct2_alloc * 100)/$reported]]
	set acct3_alloc_str [format "%d\\\(%.2f%%\\\)" $acct3_alloc [expr double($acct3_alloc * 100)/$reported]]
	set wckey1_alloc_str [format "%d\\\(%.2f%%\\\)" $wckey1_alloc [expr double($wckey1_alloc * 100)/$reported]]
	set user1_wckey1_alloc_str [format "%d\\\(%.2f%%\\\)" $user1_wckey1_alloc [expr double($user1_wckey1_alloc * 100)/$reported]]
	set user2_wckey1_alloc_str [format "%d\\\(%.2f%%\\\)" $user2_wckey1_alloc [expr double($user2_wckey1_alloc * 100)/$reported]]

	set reported [format "%d\\\(%.2f%%\\\)" $reported 100]


	send_user "\nTesting sreport for 3 days (Within: inc22.1.3)\n"
	set matches 0
	set my_pid [spawn $sreport --local cluster utilization cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,idle,down,alloc,res,over,reported]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
		    	incr exit_code 1
		}
		-re "$cluster.$idle.$down.$alloc.$resv.$over.$reported." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 1} {
		send_user "\nFAILURE:  sreport didn't give good info 17 $matches. (Within: inc22.1.3)\n"
		send_user "we are looking for $cluster.$idle.$down.$alloc.$resv.$over.$reported. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the UserUtilizationByAccount report
	set matches 0
	set my_pid [spawn $sreport --local cluster UserUtilizationByAccount cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,login,account,used]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$user2.$account3.$job2_alloc_str." {
			send_user "got 2\n"
			incr matches
			exp_continue
		}
		-re "$cluster.$user1.$account1.$job1_alloc_str." {
			send_user "got 1\n"
			incr matches
			exp_continue
		}
		-re "$cluster.$user1.$account2.$job3_alloc_str." {
			send_user "got 3\n"
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 3} {
		send_user "\nFAILURE:  sreport didn't give good info 18 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the AccountUtilizationByUser report
	set matches 0
	set my_pid [spawn $sreport --local cluster AccountUtilizationByUser cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,account,login,used]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.root..$total_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account1..$acct1_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account1.$user1.$job1_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account2..$acct2_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account2.$user1.$job3_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account3..$acct3_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account3.$user2.$job2_alloc_str." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 7} {
		send_user "\nFAILURE:  sreport didn't give good info 19 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the UserUtilizationByWckey report
	set matches 0
	set my_pid [spawn $sreport --local cluster UserUtilizationByWckey cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,login,wckey,used]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$user2.$wckey1.$user2_wckey1_alloc_str." {
			send_user "got 2\n"
			incr matches
			exp_continue
		}
		-re "$cluster.$user1.$wckey1.$user1_wckey1_alloc_str." {
			send_user "got 1\n"
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$wc_key_track == 1 && $matches != 2} {
		send_user "\nFAILURE:  sreport didn't give good info 20 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the WckeyUtilizationByUser report
	set matches 0
	set my_pid [spawn $sreport --local cluster WckeyUtilizationByUser cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,wckey,login,used]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$wckey1..$wckey1_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$wckey1.$user1.$user1_wckey1_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$wckey1.$user2.$user2_wckey1_alloc_str." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$wc_key_track == 1 && $matches != 3} {
		send_user "\nFAILURE:  sreport didn't give good info 21 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the User Top report
	set matches 0
	set my_pid [spawn $sreport --local user top cluster='$cluster' start=$start_str end=$end_str -tsecper -p -n format=cluster,account,login,used]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$account3.$user2.$job2_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account1.$user1.$job1_alloc_str." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account2.$user1.$job3_alloc_str." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 3} {
		send_user "\nFAILURE:  sreport didn't give good info 22 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the Job Size report
	set matches 0
	set my_pid [spawn $sreport --local job size grouping=2,4 cluster='$cluster' start=$start_str end=$end_str -tsec -p -n]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$account1.0.$job1_alloc.0." {
			incr matches
			exp_continue
		}
		-re "$cluster.$account2.0.$job3_alloc.$job2_alloc." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 2} {
		send_user "\nFAILURE:  sreport didn't give good info 23 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the Job Size report down a level
	set matches 0
	set my_pid [spawn $sreport --local job size grouping=2,4 cluster='$cluster' account='$account2' start=$start_str end=$end_str -tsec -p -n]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$account3.0.0.$job2_alloc." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding (Within: inc22.1.3)\n"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches != 1} {
		send_user "\nFAILURE:  sreport didn't give good info 24 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	if { $exit_code } {
		return $exit_code
	}

	# test the Job Size by wckey report
	set matches 0
	set my_pid [spawn $sreport --local job sizesbywckey grouping=2,4 cluster='$cluster' start=$start_str end=$end_str -tsec -p -n]
	expect {
		-re "There was a problem" {
			send_user "FAILURE: there was a problem with the sacctmgr command (Within: inc22.1.3)\n"
			incr exit_code 1
		}
		-re "$cluster.$wckey1.0.$wckey_alloc_sec1.$wckey_alloc_sec2." {
			incr matches
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr archive load not responding\n (Within: inc22.1.3)"
			slow_kill $my_pid
			incr exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches != 1} {
		send_user "\nFAILURE:  sreport didn't give good info 24 $matches. (Within: inc22.1.3)\n"
		incr exit_code 1
	}

	return $exit_code
}
